<!-- 切换子账号 -->
<template>
  <div id="subAccount">
    <c-title text="切换账号"></c-title>
    <van-radio-group v-model="radio" style="margin-top: 1rem;text-align: left;">
      <van-cell-group inset>
        <van-cell v-for="(item,index) in accountlist" :title="item.account||'主账号'" clickable @click="onClick(item)" :key="index">
          <template #right-icon>
            <van-icon name="edit" size="22px" style="padding: 0 14px; " @click.stop="editAccount(item)" v-if="!item.uid"/>
            <van-radio :name="item.uid||item.member_id" />
          </template>
        </van-cell>
        <van-cell title="添加账号" @click="addAccount" icon="add-o" style="color: #f15353" v-if="mainAccount.current_is_main&&mainAccount.sub_account_switch"/>
      </van-cell-group>
    </van-radio-group>
  </div>
</template>

<script>
import { scrollMixin } from "utils/mixin";
export default {
  mixins: [scrollMixin], //加载更多
  data () {
    return {
      radio: null,
      mainAccount: {},
      accountlist: []
    };
  },

  activated() {
    this.accountlist = [];
    this.radio = null;
    this.mainAccount = {};
    this.getMainData();
    this.getSubData({ page: 1 });
  },

  components: {},

  computed: {},

  mounted() {},

  methods: {
    async getMainData() {
      let { msg, result, data } = await $http.get("plugin.account-fjyx.frontend.account-manage.main-account", {},'.');
      if (result == 0) {
        return this.$toast(msg);
      }
      this.mainAccount = data;
      this.accountlist.unshift(data);
      if(this.mainAccount.current_is_main) this.radio = this.mainAccount.uid;
    },
    async getSubData(json) {
      let { msg, result, data } = await $http.get("plugin.account-fjyx.frontend.sub-account.index", json,'.');
      if (result == 0) {
        return this.$toast(msg);
      }
      this.accountlist = this.accountlist.concat(data.data);
      this.current_page = data.current_page;
      this.last_page = data.last_page;
      this.isLoadMore = true;
    },
    getMoreData() {
      if (this.current_page >= this.last_page) {
        return;
      }
      this.isLoadMore = false;
      let page = this.current_page + 1;
      this.getSubData({ page: page });
    },
    onClick(e){
      this.radio = e.uid||e.member_id;
      let url = 'plugin.account-fjyx.frontend.account-manage.logon-sub-account';
      if(e.uid) url = 'plugin.account-fjyx.frontend.account-manage.logon-main-account';//有uid，是主账号
      this.changeAccount(url,{member_id: e.uid||e.member_id});
    },
    async changeAccount(url,json={}) {
      let { msg, data} = await $http.get(url, json,'.');
      this.$toast(msg);
      localStorage.setItem("uid", json.member_id);
      if (this.fun.isCPS()) {
        if (this.fun.isIosOrAndroid() === "ios") {
          changeAccountIOS(data.token);
        } else {
          changeAccountAndroid(data.token);
        }
        return
      }
      window.location.href = this.fun.getSiteRoot() + "/addons/yun_shop/?menu#/member?i=" + this.fun.getKeyByI() + "&mid=" + json.member_id;
      // return this.$router.replace(this.fun.getUrl("member",{},{mid: json.member_id}));
    },
    async addAccount(){
      this.$router.push(this.fun.getUrl("subAccountCreat",{status: 0},{invite_code: this.mainAccount.invite_code}));
    },
    editAccount(e){
      this.$router.push(this.fun.getUrl("subAccountCreat",{status: 1},{account: e.account,invite_code: this.mainAccount.invite_code, sid: e.id}));
    }
  }
};

</script>
<style lang='scss' rel='stylesheet/scss' scoped>
</style>